package survey.action;

import java.util.List;

import net.sf.json.JSONObject;

import survey.entity.Question;
import survey.entity.Questionnaire;
import survey.service.AnswerService;
import survey.service.QuestionService;
import survey.service.QuestionnaireService;
import survey.util.QuestionUtil;

import com.opensymphony.xwork2.ActionSupport;

public class SurveyMangerAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	String resultStr="";
	private int qnId;
	private String content;
	private Questionnaire questionnaire;
	private QuestionnaireService questionnaireService;
	private QuestionService questionService;
	private AnswerService answerService;
	
	
	

	public String getResultStr() {
		return resultStr;
	}

	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public AnswerService getAnswerService() {
		return answerService;
	}

	public void setAnswerService(AnswerService answerService) {
		this.answerService = answerService;
	}

	public QuestionnaireService getQuestionnaireService() {
		return questionnaireService;
	}

	public void setQuestionnaireService(QuestionnaireService questionnaireService) {
		this.questionnaireService = questionnaireService;
	}

	public Questionnaire getQuestionnaire() {
		return questionnaire;
	}

	public void setQuestionnaire(Questionnaire questionnaire) {
		this.questionnaire = questionnaire;
	}

	public int getQnId() {
		return qnId;
	}

	public void setQnId(int qnId) {
		this.qnId = qnId;
	}

	public String createOrUpdate() {
		questionnaire=questionnaireService.getQuestionnaire(qnId);
		List<Question> questionList =questionService.queryQuestionByQnId(qnId);
		for(int i=0;i<questionList.size();i++){
			List answerList =answerService.queryAnserByQId(questionList.get(i).getId());
			questionList.get(i).setAnswerList(answerList);
		}
		questionnaire.setQuestionList(questionList);
		return SUCCESS;
	}
	public String createQuestionAjax(){
		String status="";
		JSONObject json = new JSONObject();
		if(!QuestionUtil.validateQuestionFromAjax(content)){
			status="failed";
		}else{
			status="success";
		}
		
		json.accumulate("status", status);
		
		resultStr=json.toString();
		return "json";
	}

}
